package com.acwing.partition4;

import java.io.*;
import java.util.*;

/**
 * @author `RKC`
 * @date 2022/1/12 21:46
 */
public class AC342道路与航线 {

    private static final int N = 25010, M1 = 50010 * 2, M2 = 50010, INF = 0x3f3f3f3f;
    private static int n = 0, m1 = 0, m2 = 0, index = 0;
    private static int[] head = new int[N], element = new int[M1 + M2], next = new int[M1 + M2], weight = new int[M1 + M2];
    private static int[] dist = new int[N];
    private static boolean[] visited = new boolean[N];

    //blockKeys[i]记录点所属的连通块的key，可以通过这个key在blockMap中找到下面的所有连通的点
    private static int[] blockKeys = new int[N];
    private static Map<Integer, List<Integer>> blockMap = new HashMap<>();
    private static Map<Integer, Integer> blockInDegree = new HashMap<>();

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] str = reader.readLine().split("\\s+");
        n = Integer.parseInt(str[0]);
        m1 = Integer.parseInt(str[1]);
        m2 = Integer.parseInt(str[2]);
        int s = Integer.parseInt(str[3]);
        for (int i = 0; i <= n; i++) head[i] = -1;
        //输入所有的道路
        for (int i = 0; i < m1; i++) {
            str = reader.readLine().split("\\s+");
            int u = Integer.parseInt(str[0]), v = Integer.parseInt(str[1]), w = Integer.parseInt(str[2]);
            createEdge(u, v, w);
            createEdge(v, u, w);
        }
        //给道路内的点与点进行编号，表示属于某个连通块，可以使用bfs/dfs/并查集
        bfs();
        //输入所有的航线
        for (int i = 0; i < m2; i++) {
            str = reader.readLine().split("\\s+");
            int u = Integer.parseInt(str[0]), v = Integer.parseInt(str[1]), w = Integer.parseInt(str[2]);
            //给v所在的连通块的入度加1
            blockInDegree.put(blockKeys[v], blockInDegree.get(blockKeys[v]) + 1);
            createEdge(u, v, w);
        }
        //拓扑排序
        topological(s);
        for (int i = 1; i <= n; i++) {
            if (dist[i] > INF / 2) writer.write("NO PATH\n");
            else writer.write(dist[i] + "\n");
        }
        writer.flush();
    }

    private static void topological(int start) {
        for (int i = 1; i <= n; i++) dist[i] = INF;
        dist[start] = 0;
        //对每个连通块之间进行拓扑排序，拓扑排序中进行Dijkstra算法计算
        Queue<Integer> queue = new LinkedList<>();
        //把所有入度为0的连通块加入到队列中
        for (Map.Entry<Integer, Integer> entry : blockInDegree.entrySet()) {
            if (entry.getValue() == 0) queue.add(entry.getKey());
        }
        while (!queue.isEmpty()) {
            int blockKey = queue.poll();
            //dijkstra操作的拓扑排序队列
            dijkstra(blockKey, queue);
        }
    }

    private static void dijkstra(int blockKey, Queue<Integer> queue) {
        PriorityQueue<int[]> heap = new PriorityQueue<>((o1, o2) -> Integer.compare(o1[1], o2[1]));
        //将所有这个连通块下的点都加入到堆中
        for (int u : blockMap.get(blockKey)) heap.add(new int[]{u, dist[u]});
        while (!heap.isEmpty()) {
            int[] pair = heap.poll();
            int u = pair[0];
            if (visited[u]) continue;
            visited[u] = true;
            for (int i = head[u]; i != -1; i = next[i]) {
                int v = element[i];
                if (dist[v] > dist[u] + weight[i]) {
                    dist[v] = dist[u] + weight[i];
                    //如果扩展的点v和u是在同一个连通块，才可以添加到堆中
                    if (blockKeys[u] == blockKeys[v]) heap.add(new int[]{v, dist[v]});
                }
                //如果不再一个连通块的话，说明该边是一条航线
                if (blockKeys[u] != blockKeys[v]) {
                    int inDegree = blockInDegree.get(blockKeys[v]);
                    blockInDegree.put(blockKeys[v], inDegree - 1);
                    //如果连通块的入度为0，就加入到拓扑排序的队列中
                    if (inDegree - 1 == 0) queue.add(blockKeys[v]);
                }
            }
        }
    }

    private static void bfs() {
        int blockKey = 1;
        boolean[] visited = new boolean[n + 1];
        for (int i = 1; i <= n; i++) {
            //以当前节点进行bfs求连通块
            if (blockKeys[i] != 0) continue;
            Queue<Integer> queue = new LinkedList<>();
            queue.add(i);
            List<Integer> block = new ArrayList<>();
            blockMap.put(blockKey, block);
            blockInDegree.put(blockKey, 0);
            while (!queue.isEmpty()) {
                int u = queue.poll();
                if (visited[u]) continue;
                visited[u] = true;
                //节点分配连通块blockKey，并加入到连通块中进行管理
                blockKeys[u] = blockKey;
                block.add(u);
                for (int j = head[u]; j != -1; j = next[j]) {
                    int v = element[j];
                    queue.add(v);
                }
            }
            blockKey++;
        }
    }

    private static void createEdge(int u, int v, int w) {
        element[index] = v;
        next[index] = head[u];
        head[u] = index;
        weight[index++] = w;
    }
}
